const Goods = require("../model/goods.model");
const Category = require("../model/category.model");
const { Op } = require("sequelize");
// 雪花id生成
const SNOWFLAKE = require("@zsea/snowflake");
const snowflake = new SNOWFLAKE({
  type: "string",
});

class GoodsService {
  async createGoods({
    goodsName,
    goodsPrice,
    goodsNum,
    category,
    goodsDesc,
    imgUrl,
  }) {
    try {
      const res = await Goods.create({
        product_id: snowflake.nextId(),
        product_name: goodsName,
        product_price: goodsPrice,
        product_num: goodsNum,
        product_type: category,
        product_state: "0",
        product_desc: goodsDesc,
        product_img: imgUrl,
        is_hot: "0",
        is_new: "1",
      });
      if (res) {
        return {
          code: "10000",
          message: "商品添加成功",
          data: {},
        };
      }
    } catch (error) {
      console.log(error);
    }
  }
  async getGoodsList(pageNum, pageSize, goodsName) {
    // console.log("获取商品列表", pageNum, pageSize);
    const offset = (pageNum - 1) * pageSize;
    try {
      // 如果有商品名
      if (goodsName) {
        // 获取总数，模糊查询
        const total = await Goods.count({
          where: {
            product_name: {
              [Op.like]: `%${goodsName}%`,
            },
          },
        });
        const res = await Goods.findAll({
          limit: pageSize * 1,
          offset,
          where: {
            product_name: {
              [Op.like]: `%${goodsName}%`,
            },
          },
        });
        return {
          pageNum,
          pageSize,
          total,
          list: res,
        };
      } else {
        const total = await Goods.count();
        const res = await Goods.findAll({
          limit: pageSize * 1,
          offset,
        });
        return {
          pageNum,
          pageSize,
          total,
          list: res,
        };
      }
    } catch (error) {
      // console.log(error);
    }

    // 获取分页数
    // if (type === "all") {
    //   try {
    //     const res = await Goods.findAll({
    //       raw: true,
    //       // limit: 5,
    //       offset: 5,
    //     });
    //     console.log("商品获取结果", res);
    //     return res;
    //   } catch (error) {
    //     console.log(error);
    //   }
    // }
  }

  async updateGoods(
    id,
    goodsName,
    goodsPrice,
    goodsNum,
    category,
    goodsDesc,
    imgUrl
  ) {
    try {
      const res = await Goods.update(
        {
          product_name: goodsName,
          product_price: goodsPrice,
          product_num: goodsNum,
          product_type: category,
          product_desc: goodsDesc,
          product_img: imgUrl,
        },
        {
          where: {
            product_id: id,
          },
        }
      );
      return res[0] > 0 ? true : false;
    } catch (error) {
      console.log(error);
    }
  }

  async checkGoods(id, state) {
    try {
      const res = await Goods.update(
        {
          product_state: state,
        },
        {
          where: {
            product_id: id,
          },
        }
      );
      return res[0] > 0 ? true : false;
    } catch (error) {
      console.log(error);
    }
  }

  async removeGoods(id) {
    try {
      const res = await Goods.destroy({
        where: {
          product_id: id,
        },
      });
      return res > 0 ? true : false;
    } catch (error) {
      console.log(error);
    }
  }

  // 获取商品分类
  async getGoodsType() {
    try {
      const res = await Category.findAll({});
      return res;
    } catch (error) {
      console.log(error);
    }
  }
}

module.exports = new GoodsService();
